// pages/login/Bindphone/Bindphone.js
const app = getApp();
var interval = null //倒计时函数
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:'',
    date: '请选择日期',
    fun_id: 2,
    time: '获取验证码', //倒计时 
    currentTime: 61,
    phone: '',
    code: '',
    userid: [],
    img:false,
  },
  img:function(){
    var that = this;
    that.setData({
      img:true
    })
  },
  img1: function () {
    var that = this;
    that.setData({
      img: false
    })
  },
  bindTextcode: function (e) {
    this.setData({
      code: e.detail.value
    })
  },
  bindTextphone: function (e) {
    this.setData({
      phone: e.detail.value
    })
    console.log(e)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var id = app.globalData.userid;
    console.log(id);
  },
  getCode: function (options) {
    var that = this;
    var currentTime = that.data.currentTime
    interval = setInterval(function () {
      currentTime--;
      that.setData({
        time: currentTime + '秒'
      })
      if (currentTime <= 0) {
        clearInterval(interval)
        that.setData({
          time: '重新发送',
          currentTime: 61,
          disabled: false
        })
      }
    }, 1000)
  },
  getVerificationCode() {
    this.getCode();
    var that = this;
    that.setData({
      disabled: true
    })
  },
  formSubmit: function (e) {
    var phone = this.data.phone;
    var code = this.data.code;
    if (phone == '' || code == '') {
      wx.showToast({
        title: '请输入手机号或验证码',
        icon: '',
        image: '',
        duration: 1000,
        mask: true,
      })
    }
    if (phone.length !== 11) {
      wx.showToast({
        title: '手机号长度有误！',
        image: '',
        duration: 1500
      })
      return false;
    }
    if (code.length !== 6) {
      wx.showToast({
        title: '验证码长度有误！',
        image: '',
        duration: 1500
      })
      return false;
    }
    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    if (!myreg.test(phone, code)) {
      wx.showToast({
        title: '手机号或验证码格式有误！',
        image: '',
        duration: 1500
      })
      return false;
    }
    else {
      wx.showToast({
        title: '绑定成功',
        icon: '',
        image: '',
        duration: 2000,
        mask: true,
      })
      var that = this; 
      that.setData({
        phone:'',
        code:'',
      }) 
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})